<?php

include DIRETORIO."app/Models/GraphicDAO.php";
include DIRETORIO."app/Libraries/Graph/pData.class";   
include DIRETORIO."app/Libraries/Graph/pChart.class";

class C_Graphic{
	
	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

	public static function getBurndown($post){		
		try{
			$objGraphicDAO   = new GraphicDAO(self::getDB());

			$idSprint = $post["idSprint"];
			if($idSprint == 0){
				$objSprintDAO	= new SprintDAO(self::getDB());
				$objSprint		= $objSprintDAO->actualSprint($_SESSION["PROJECT"]->id);
				$idSprint		= $objSprint->id;
			}

			$objHoursSprints = $objGraphicDAO->getHoursSprint($idSprint);
			
			//Calcula a linha ideal do Burndown
			if(count($objHoursSprints) > 0){				
				$sprinsDays  = (int)$objHoursSprints[0]->days;
				$sprinsHours = (int)$objHoursSprints[0]->hours;

				if($sprinsDays > 0 && $sprinsHours > 0)
					$hourPerDay = $sprinsHours/$sprinsDays;
				else
					$hourPerDay = 0;
				
				$date = new DateTime($objHoursSprints[0]->dateBegin);
				for($i=0; $i<=$sprinsDays; $i++){
					$dataDefault[] = $sprinsHours-round($hourPerDay*$i);
					$period[$date->format('d/m')] = $date->format('d/m');
					$date->modify('+1 day');
				}
			}

			//Calcula a linha real de trabalho do Burndown
			if($sprinsDays > 0){
				$dateSprint		= new DateTime($objHoursSprints[0]->dateBegin);
				$objHoursWorket	= $objGraphicDAO->getHoursWorket($idSprint);

				if(count($objHoursWorket) > 0){
					$dateLastGoing	= new DateTime($objHoursWorket[count($objHoursWorket)-1]->dateGoing);
					$interval		= $dateSprint->diff($dateLastGoing);
					$goingDays		= $interval->format('%d'); 
				}			

				for($i=0; $i<=$goingDays; $i++){
					$flagAddDate = true;
					
					while(list($key, $row) = each($objHoursWorket)){
						$dateGoing = new DateTime($row->dateGoing);

						if($dateSprint == $dateGoing){							
							$sprinsHours -= round($row->hours);
							$dataHours[] = $sprinsHours;
							unset($objHoursWorket[$key]);
							$flagAddDate = false;
							break;
						}
					} 
					
					reset($objHoursWorket);

					if($flagAddDate)
						$dataHours[] = $sprinsHours;

					$period[$dateSprint->format('d/m')] = $dateSprint->format('d/m');
					$dateSprint->modify('+1 day');
				}
			}

			// Dataset definition   
			$DataSet = new pData;
			$DataSet->AddPoint($dataDefault,"dataDefault");
			$DataSet->AddPoint($period,"period");
			$DataSet->AddPoint($dataHours,"dataHours");
			$DataSet->AddSerie("dataDefault");
			$DataSet->AddSerie("dataHours");
			$DataSet->SetAbsciseLabelSerie("period");
			$DataSet->SetSerieName("Sprint ".$objHoursSprints[0]->order,"dataHours");
			$DataSet->SetSerieName("Esforço Ideal","dataDefault");
			$DataSet->SetYAxisUnit(" Hrs");
			  
			// Initialise the graph
			$sizeGraphWidth = 800;
			$sizeGraphHeight = 400;

			$Test = new pChart($sizeGraphWidth+30,$sizeGraphHeight+30);  
			$Test->setFontProperties(DIRETORIO."app/Libraries/Fonts/tahoma.ttf",8);  
			$Test->setGraphArea(60,30,$sizeGraphWidth,$sizeGraphHeight);  
			$Test->drawGraphArea(252,252,252);
			$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,0);
			$Test->drawGrid(4,TRUE,230,230,230,255);
			  
			// Draw the line graph  
			$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
			
			$tempData = $DataSet->GetData();
			foreach($tempData as $row){
				unset($row['dataDefault']);
				$dataNoDefaultLine[] = $row;
			}

			$Test->drawPlotGraph($dataNoDefaultLine,$DataSet->GetDataDescription(),3,2,255,255,255);  
			  
			// Finish the graph  
			$Test->setFontProperties(DIRETORIO."app/Libraries/Fonts/tahoma.ttf",8);  
			$Test->drawLegend($sizeGraphWidth-150,80,$DataSet->GetDataDescription(),255,255,255);  
			$Test->setFontProperties(DIRETORIO."app/Libraries/Fonts/tahoma.ttf",10);  
			$Test->drawTitle(60,20,"Burndown",50,50,50);  
			$Test->Render(DIRETORIO."content/graphic/burndonw.png");

			$return = array("true","burndonw.png?date=".date("Ymdhis"));
		}catch(Exception $e){
			$return = array("false","Não foi possível gerar o gráfico!");
		}

		return json_encode($return);
	}
}